/*
 * communication.c
 *
 *  Created on: 08.03.2011
 *      Author: nick
 */

#include "communication.h"

void initUart(void) {
    /*
     * Konfiguriere UART1
     * Kommunikation mit Sensor-Master
     */
    U1MODE = 0;
    U1STA = 0;
    U1BRG = BRGVAL;
    U1MODEbits.UARTEN = 1;
    U1STAbits.UTXEN = 0;

    IFS0bits.U1RXIF = 0;
    IEC0bits.U1RXIE = 1;
}

void initTimer(void)
{
  /*
   * Konfiguriere Timer 2
   * bewirkt Idle-Line-Timeout (280us)
   */
  T2CON = 0;
  PR2 = IDLE_LINE_TIMEOUT;
  IFS0bits.T2IF = 0;
  IEC0bits.T2IE = 0;
  IPC1bits.T2IP = 1;
  TMR2 = 0;
  T2CONbits.TON = 1;

  //Konfiguriere Timer 1
  T1CON = 0;
  T1CONbits.TCKPS = 0b10;
  // 11562,5 entspricht 20ms
  // 35k entspr. ca. 3 PWM Perioden
  PR1 = 35000;
  IFS0bits.T1IF = 0;
  IEC0bits.T1IE = 0;
  IPC0bits.T1IP = 1;
  TMR1 = 0;
  T1CONbits.TON = 1;
}

void initChangeNotification(void)
{
	//Change Notification Interrupt
	IFS1bits.CNIF = 0;
	IPC4bits.CNIP = 1;
	CNEN2bits.CN30IE = 1;
	TRISAbits.TRISA2 = 1;
	IEC1bits.CNIE = 1;
}

// send Sensor Data valueMax/Min = [16384, -16383] if the val is bigger/smaller it gets cut off
void sendSensorData(ESensorAdr addr, ESensorValClass valClass, int value, int alarm) {
    char byte1, byte2, byte3;
    //prepare the data
    //Sensoraddr + Valueclass
    byte1 = addr << 4;
    byte1 = byte1 | valClass;
    // Sensorvalue
    //Lowbyte(7Bit) + Alarmflag
    byte2 = value << 1;
    //if the alarm flag is set, set the last bit to 1
    if (alarm)
        byte2 = byte2 | 0x01;
    //Highbyte
    byte3 = value >> 7;
    //check if the value was positiv/negativ and change the sign accordingly
    //TODO integer größe prüfen
    if (value >> 15 == 1)
        byte3 = byte3 | 0x01 << 7;

    //Disable UART-RX Interrupt
    IEC0bits.U1RXIE = 0;

    //Enable UART1 Transmitter
    U1STAbits.UTXEN = 1;
    TRISBbits.TRISB6 = 0;
    //Send the data
    while(U1STAbits.UTXBF);
    U1TXREG = byte1;
    while(U1STAbits.UTXBF);
    U1TXREG = byte2;
    while(U1STAbits.UTXBF);
    U1TXREG = byte3;

    while(!U1STAbits.TRMT);
    
    //Enable Receiver & Disable Transmitter
    U1STAbits.UTXEN = 0;
    TRISBbits.TRISB6 = 1;
    IFS0bits.U1RXIF = 0;
    IEC0bits.U1RXIE = 1;

    //über Loopback empfangene Zeichen löschen TODO: vermutlich unnötig - entfernen und testen
    while (DataRdyUART1()) {
        getcUART1();
    }
}

//TODO: funktion testen
void sendArray( char *array , int size)
{
  //Disable UART-RX Interrupt
  IEC0bits.U1RXIE = 0;
  //Enable UART1 Transmitter
  U1STAbits.UTXEN = 1;
  TRISBbits.TRISB6 = 0;

  //write the data to the to-be-send register
  int i;
  for(i=0; i < size ; i++ ){
    while(U1STAbits.UTXBF);
    U1TXREG = array[i];
  }

  //wait while transceiver is busy
  while(!U1STAbits.TRMT);
  //Enable Receiver & Disable Transmitter
  U1STAbits.UTXEN = 0;
  TRISBbits.TRISB6 = 1;
  IFS0bits.U1RXIF = 0;
  IEC0bits.U1RXIE = 1;

  //über Loopback empfangene Zeichen löschen TODO: vermutlich unnötig - entfernen und testen
  while (DataRdyUART1()) {
      getcUART1();
  }
}
